var unitsenter = new Vue({
    el:'#unitsenter',
    //初始化数据类型
    data:{
        //采购单位入驻信息
        unitEnter:{
            text:'',
        },
        //采购办集合信息
        optionsUser:[],
        //获取到的验证码信息
        securityCode: '',
        //用户填写的验证码
        securityCodeUser: '',
        //表单标题判断
        title:'采购单位入驻',
        //表单按钮判断
        buttom:'添加',
        //判断表单是否验证
        verify:'',
    },
    //时间提交方法
    methods:{
        //采购单位入驻
        addUnitEnter:function(){
            var _this = this;
            if(_this.unitEnter.sex=="男"){
                _this.unitEnter.sex="0"
            }else{
                _this.unitEnter.sex="1"
            }
            _this.unitEnter.address=$("#province1").val()+"-"+$("#city1").val()+"-"+$("#district1").val()+"-"+ _this.unitEnter.text;
                $.post("/sys/user/addUnitEnter",{"user":JSON.stringify(_this.unitEnter)},function(data){
                    if(data.code==200){
                        _this.$message({
                            message: '恭喜你，信息已录入！',
                            type: 'success'
                        });
                        _this.unitEnter={};
                        $("#button").attr("disabled","disabled");
                    }else{
                        _this.$message({
                            message: '警告哦，信息未录入！',
                            type: 'warning'
                        });
                    }
                });

        },
        //获取手机验证码
        telCode:function(){
            var _this = this;
            $("#input3").attr("disabled","disabled");
            $.post("/home/warnInfo/sendInfo",{"tel":_this.unitEnter.telephone},function(data){
                if (data != "500") {
                    _this.securityCode = data;
                    _this.$message.success("验证码已发送!");
                    sendMessage();
                } else {
                    _this.$message.error("获取验证码失败！");
                }
            });
        },
    },
    //页面加载完成后执行此方法
    created :function(){
        var _this = this;
        //采购单位按钮数据清空
            _this.optionsUser.length;
            _this.unitEnter={};
            _this.optionsUser=[];
            if(this.optionsUser.length == 0){
                $.post("/sys/user/seleceOfficeUser", "", function (data) {
                    for (var i = 0; i < data.data.sysUser.length; i++) {
                        debugger
                        var map1 = {};
                        map1.value = data.data.sysUser[i].id;
                        map1.label = data.data.sysUser[i].name;
                        _this.optionsUser.push(map1);
                    }
                });
            }
    }
});
var sysuser;
$(document).ready(function(){
    $("#code").attr("disabled","disabled");
    $("#button").attr("disabled","disabled");
    $.post("/sys/user/selectUserForm","",function(data){
        sysuser = data.data.sysUser;
    });
});
//提交按钮状态设置
function enableSubmit(bool){
    if(bool) {
        $("#button").removeAttr("disabled");
    } else {
        $("#button").attr("disabled","disabled");
    }
}
//手机号码验证
var RegTel = /^1[0-9]\d{9}$/;
//邮箱验证的正则
var RegEmail = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;

//验证多少项，多少个false
var flags = [false,false,false,false,false,false,false,false,false,false,false];
//flags全为true时提交按钮解除禁用
function v_submitbutton(){
    for(f in flags) if(!flags[f]) {
        enableSubmit(false);
        return;
    }
    enableSubmit(true);
}
//验证帐号
function v_username(){
    debugger
    var username = $("#input1").val();
    var arr = [];
    for(var i=0;i<sysuser.length;i++){
        arr.push(sysuser[i].username);
    }
    if($.inArray(username, arr)>=0) { flags[0]=false;enableSubmit(false);
        $("#tabUnit-1").html("帐号已存在");$("#img-iconUnit-12").css("display","block");$("#input1").css("border","solid 1px red");$("#tabUnit-1").css("display","block")}
    else {
        if (username.trim() == "") {
            flags[0] = false;
            enableSubmit(false);
            $("#tabUnit-1").html("帐号不能为空");
            $("#img-iconUnit-12").css("display", "block");
            $("#input1").css("border", "solid 1px red");
            $("#tabUnit-1").css("display", "block")
        } else {
            flags[0] = true;
            $("#input1").css("border", "solid 1px #2dc12d", "");
            $("#img-iconUnit-11").css("display", "block");
            $("#tabUnit-1").css("display", "none");
            $("#img-iconUnit-12").css("display", "none");
        }
        v_submitbutton();
    }
}
//验证单位名称
function v_name(){
    var name = $("#input2").val();
    if(name.trim()=="") { flags[1]=false;enableSubmit(false);
        $("#tabUnit-2").html("单位名称");$("#img-iconUnit-22").css("display","block");$("#input2").css("border","solid 1px red");$("#tabUnit-2").css("display","block")}
    else{flags[1] = true;
        $("#input2").css("border","solid 1px #2dc12d","");$("#img-iconUnit-21").css("display","block");$("#tabUnit-2").css("display","none");$("#img-iconUnit-22").css("display","none");}
    v_submitbutton();
}
//验证姓名
function v_xmname(){
    var name = $("#input5").val();
    if(name.trim()=="") { flags[8]=false;enableSubmit(false);
        $("#tabUnit-5").html("姓名不能为空");$("#img-iconUnit-52").css("display","block");$("#input5").css("border","solid 1px red");$("#tabUnit-5").css("display","block")}
    else{flags[8] = true;
        $("#input5").css("border","solid 1px #2dc12d","");$("#img-iconUnit-51").css("display","block");$("#tabUnit-5").css("display","none");$("#img-iconUnit-52").css("display","none");}
    v_submitbutton();
}
//验证预算金额
function v_budgetAmount(){
    var name = $("#input9").val();
    if(name.trim()=="") { flags[9]=false;enableSubmit(false);
        $("#tabUnit-9").html("年初预算不能为空");$("#img-iconUnit-92").css("display","block");$("#input9").css("border","solid 1px red");$("#tabUnit-9").css("display","block")}
    else{flags[9] = true;
        $("#input9").css("border","solid 1px #2dc12d","");$("#img-iconUnit-91").css("display","block");$("#tabUnit-9").css("display","none");$("#img-iconUnit-92").css("display","none");}
    v_submitbutton();
}
//验证结余金额
function v_balanceAmount(){
    var name = $("#input10").val();
    if(name.trim()=="") { flags[10]=false;enableSubmit(false);
        $("#tabUnit-10").html("结余金额不能为空");$("#img-iconUnit-102").css("display","block");$("#input10").css("border","solid 1px red");$("#tabUnit-10").css("display","block")}
    else{flags[10] = true;
        $("#input10").css("border","solid 1px #2dc12d","");$("#img-iconUnit-101").css("display","block");$("#tabUnit-10").css("display","none");$("#img-iconUnit-102").css("display","none");}
    v_submitbutton();
}
//验证手机
function v_telphone() {
    var telphone = $("#input3").val();
    if (!RegTel.test(telphone)) {
        flags[2] = false;
        enableSubmit(false);
        $("#tabUnit-3").html("手机号为11位");
        $("#img-iconUnit-32").css("display", "block");
        $("#tabUnit-3").css("display", "block");
        $("#input3").css("border", "solid 1px red");
        $("#code").attr("disabled","disabled");
        $("#input71").val('');
    }
    else {
        flags[2] = true;
        $("#input3").css("border", "solid 1px #2dc12d", "");
        $("#img-iconUnit-31").css("display", "block");
        $("#tabUnit-3").css("display", "none");
        $("#img-iconUnit-32").css("display", "none");
        $("#input71").val('');
        $("#code").removeAttr("disabled");
    }
    v_submitbutton();
}
//验证性别
function v_sex(){
    var sex = $("#input51").val();
    if(sex!="男"&&sex!="女") {flags[8]=false;enableSubmit(false);
        $("#tabUnit-51").html("必输项");$("#img-iconUnit-512").css("display","block");$("#input51").css("border","solid 1px red");}
    else{flags[8] = true;
        $("#input51").css("border","solid 1px #2dc12d","");$("#img-iconUnit-511").css("display","block");$("#tabUnit-51").css("display","none");$("#img-iconUnit-512").css("display","none");}
    v_submitbutton(true);

}
//验证邮箱
function v_email() {
    var email = $("#input4").val();
    if (!RegEmail.test(email)) {
        flags[3] = false;
        enableSubmit(false);
        $("#tabUnit-4").html("邮箱格式不正确");
        $("#img-iconUnit-42").css("display", "block");
        $("#tabUnit-4").css("display", "block");
        $("#input4").css("border", "solid 1px red");
    }
    else {
        flags[3] = true;
        $("#input4").css("border", "solid 1px #2dc12d", "");
        $("#img-iconUnit-41").css("display", "block");
        $("#tabUnit-4").css("display", "none");
        $("#img-iconUnit-42").css("display", "none");
    }
    v_submitbutton();
}
//验证地址
function v_address(){
    v_createBy();
    var selected = document.getElementsByClassName("form-control select");
    for (var i = 0; i < selected.length; i++) {
        if (selected[i].value == "") {
            flags[4] = false;
            enableSubmit(false);
            $("#province1,#city1,#district1").css("border", "solid 1px red");
        } else {
            flags[4] = true;
            $("#province1,#city1,#district1").css("border", "solid 1px #2dc12d", "");
        }
        v_submitbutton();
    }

}
//验证采购区域
$(".el-input__inner").on("blur",v_createBy);
function v_createBy(){
    var createBy = $(".el-input__inner").val();
    if(createBy.trim()==""){
        flags[5] = false;
        enableSubmit(false);
        $("#tabUnit-6").html("请选择采购区域");
        $("#img-iconUnit-62").css("display", "block");
        $("#tabUnit-6").css("display", "block");
        $(".el-input__inner").css("border", "solid 1px red");
    }else{
        flags[5] = true;
        $(".el-input__inner").css("border", "solid 1px #2dc12d", "");
        $("#img-iconUnit-61").css("display", "block");
        $("#tabUnit-6").css("display", "none");
        $("#img-iconUnit-62").css("display", "none");
    }
    v_submitbutton();
}
//验证验证码
function v_code() {
    var securityCodeUser = $("#input71").val();
    if (securityCodeUser != unitsenter.securityCode || securityCodeUser.trim() =="") {
        flags[6] = false;
        enableSubmit(false);
        $("#tabUnit-7").html("请输入4位验证码");
        $("#img-iconUnit-72").css("display", "block");
        $("#tabUnit-7").css("display", "block");
        $("#input71").css("border", "solid 1px red");
        $("#input71").removeAttr("disabled");

    }
    else {
        flags[6] = true;
        $("#input71").css("border", "solid 1px #2dc12d", "");
        $("#img-iconUnit-71").css("display", "block");
        $("#tabUnit-7").css("display", "none");
        $("#img-iconUnit-72").css("display", "none");
        $("#input71").attr("disabled","disabled");
        $("#input3").attr("disabled","disabled");
        $("#code").attr("disabled","disabled");
        window.clearInterval(InterValObj);
    }
    v_submitbutton();
}
function v_addtext() {
    var securityCodeUser = $("#text").val();
    if (securityCodeUser.trim() =="") {
        flags[7] = false;
        enableSubmit(false);
        $("#tabUnit-8").html("请输入详细地址");
        $("#img-iconUnit-82").css("display", "block");
        $("#tabUnit-8").css("display", "block");
        $("#text").css("border", "solid 1px red");
    }
    else {
        flags[7] = true;
        $("#text").css("border", "solid 1px #2dc12d", "");
        $("#img-iconUnit-81").css("display", "block");
        $("#tabUnit-8").css("display", "none");
        $("#img-iconUnit-82").css("display", "none");
    }
    v_submitbutton();
}

var InterValObj; //timer变量，控制时间
var count = 120; //间隔函数，1秒执行
var curCount;//当前剩余秒数

function sendMessage() {
    curCount = count;
    //设置button效果，开始计时
    $("#code").attr("disabled", "disabled");
    $("#code").val(curCount+"秒");
    InterValObj = window.setInterval(SetRemainTime, 1000); //启动计时器，1秒执行一次

    //请求后台发送验证码 TODO

}
//timer处理函数
function SetRemainTime() {
    if (curCount == 0) {
        window.clearInterval(InterValObj);//停止计时器
       if(!flags[6]){
           $("#input3").removeAttr("disabled");
           $("#code").removeAttr("disabled");
       }
        $("#code").val("获取验证码");
    }
    else {
        curCount--;
        $("#code").val(curCount+"秒");
    }
}
function adaptValue(){
    return true;
}
